﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AcsCommon;
using YiShi.Class;

namespace YiShi.KnowledgePoint
{
    public partial class PointEdit : System.Web.UI.Page
    {
        DbAccess db;
        DbEnum de;
        protected string id;
        protected string name;
        protected string typeHtml;
        protected string returnActionType;
        public PointEdit()
        {
            db = DbAccess.Get("Admin");
            de = DbEnum.Get(db, "Tree.Type");
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            JsonObject jo = new JsonObject();
            try
            {
                string action = Request["ActionType"] == null ? "" : Request["ActionType"];
                switch (action.ToLower())
                {
                    case "dataupdate":
                    case "datadelete":
                        DataUpdate();
                        break;
                    case "datainsert":
                    default:
                        DataInsert();
                        break;
                }
            }
            catch (Exception e1)
            {
                jo.Add("IsSuccess", "false");
                jo.Add("Message", e1.Message);
                jo.Add("Source", e1.Source);
                jo.Add("StackTrace", e1.StackTrace);
            }
            finally
            {
                if (jo.Count > 0)
                {
                    Response.Write(jo.ToString().Replace("'", "\""));
                    Response.End();
                }
            }
        }
        private JsonObject Update()
        {
            JsonObject jo = new JsonObject();
            YiShi.Class.Question question = new YiShi.Class.Question(db);
            question.Fill(Request);
            question.Update();
            jo.Add("IsSuccess", "true");
            jo.Add("Id", question.Id);
            return jo;
        }

        private void DataUpdate()
        {
            switch (Request["ActionType"].ToLower())
            {
                case "dataupdate":
                    returnActionType = "Update";
                    break;
                case "datadelete":
                    returnActionType = "Delete";
                    break;
            }
            id = this.Request["Id"];
            Class.KnowledgePoint kp = new YiShi.Class.KnowledgePoint(db);
            kp.FillFromDb(id);
            name = kp.Name;
            typeHtml = Utility.GetSelectHtml(de, "Type", kp.Type);
        }

        private void DataInsert()
        {
            returnActionType = "Insert";
            typeHtml = Utility.GetSelectHtml(de, "Type");
        }
    }
}
